<?php
App::uses('AppModel', 'Model');
/**
 * Player Model
 *
 * @property Character $Character
 */
class Player extends AppModel {
	public $displayField = 'plin';
	public $virtualFields = array(
		'name' => "CONCAT(Player.plin, ' - ', Player.first_name, ' ', Player.insertion, ' ', Player.surname)"
	);
//TODO: Fix double space in case of no-insertion.

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'plin' => array(
			'numericPlin' => array(
				'rule' => 'naturalNumber',
				'message' => 'PLayer Identification Number must be numeric',
				'required' => true,
			),
			'uniquePlin' => array(
				'rule' => 'isUnique',
				'message' => 'PLayer Identification Number must be unique',
				'required' => true,
			)
		),
		'account_type' => array(
			'allowedChoice' => array(
				'rule' => array('inList', array('Participant','Referee','Infobalie','Super')),
				'message' => 'Account type must be either Participant, Referee, Infobalie or Super'
			)
		),
		'username' => array(
			'alphaNumeric' => array(
				'rule'	=> 'alphaNumeric',
				'message' => 'Letters and numbers only',
				'allowEmpty' => true
			),
			'between' => array(
				'rule' => array('between', 5, 50),
				'message' => 'Between 5 and 50 characters',
				'allowEmpty' => true
			)
		),
		'password' => array(
			'rule'	=> array('minlength', 8),
			'message' => 'Minimum 8 characters long',
			'allowEmpty' => true
		),
		'first_name' => array(
			'rule' => 'notEmpty',
			'message' => 'First name is required',
		),
		'insertion' => array(),
		'surname' => array(
			'rule'	=> 'notEmpty',
			'message' => 'Surname is required',
		),
		'gender' => array(
			'allowedChoice' => array(
				'rule' => array('inList', array('M','F')),
				'message' => 'Either an M for male or a F for female',
				'allowEmpty' => true
			)
		),
		'date_of_birth' => array(
			'rule' => 'date',
			'message' => 'Enter a valid date',
			'allowEmpty' => true
		),
		'email' => 'email',
//TODO		'phone' => 'phone',
//TODO: Add regex for dutch/belgian/etc phonenumbers instead of us.
		'comments' => array(),
		'deleted' => array(
			'boolean' => array(
				'rule' => array('boolean'),
				'message' => 'You should not be seeing this message. Deleted should be true or false.',
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Character' => array(
			'className' => 'Character',
			'foreignKey' => 'player_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

/**
 * Builds search conditions for Players.
 * Function has knowledge of different search types for this model.
 * Search types are used in the view by the call to QuickSearchHelper.
 * 
 * @param array with search conditions $data
 * @return multitype: array with CakePHP search conditions
 */
	function buildConditions($data) {
		if(array_key_exists('Search', $data)) {
			$data = $data['Search'];
			$type = $data['searchType'];
			$value = $data['searchValue'];
		} else {
			$type = -1;
		}

		switch ($type) {
			case 0 :
				$conditions = array('Player.plin LIKE' => "%$value%");
				break;
			case 1 :
				$conditions = array('Player.first_name LIKE' => "%$value%");
				break;
			case 2 :
				$conditions = array('Player.surname LIKE' => "%$value%");
				break;
			case 3 :
				$conditions = array('Player.email LIKE' => "%$value%");
				break;
			case 4 :
				$conditions = array(
				"OR" => array(
				'Player.first_name LIKE' => "%$value%",
				'Player.surname LIKE' => "%$value%",
				'Player.email LIKE' => "%$value%",
				)
				);
				break;
			default :
				$conditions = array();
		}
		return $conditions;
	}

}
